package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;

import java.util.ArrayList;

public class Main5Activity extends AppCompatActivity  {
    private CandleStickChart candlechart;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        /*第一步实例化*/
        candlechart = findViewById(R.id.candlechart);
        setTitle("k线图");

        candlechart.getLegend().setEnabled(false);
        candlechart.getDescription().setEnabled(false);

        /*2. */

        ArrayList <CandleEntry>list=new ArrayList<>();
        for (int i=0;i<10;i++){ //
            float val=(float) (Math.random()*40);
            float high=(float)(Math.random()*9)+8f;//最高价
            float low=(float)(Math.random()*9)+8f;//最低价
            float open=(float)(Math.random()*6)+1f;//开盘价
            float close=(float)(Math.random()*6)+1f;//关盘价

            boolean even=i%2==0;
            list.add(new CandleEntry(
                    i,val+high,
                    val-low,
                    even?val+open:val-open,
                    even?val-close:val+close
            ));
        }
        /*x轴*/
//        XAxis xAxis = candlechart.getXAxis();
//        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//        xAxis.setDrawGridLines(false);
//        /*Y轴*/
//        YAxis leftAxis = candlechart.getAxisLeft();
//        leftAxis.setLabelCount(7, false);
//        leftAxis.setDrawGridLines(false);
//        leftAxis.setDrawAxisLine(false);
//
//        YAxis rightAxis = candlechart.getAxisLeft();
//        rightAxis.setEnabled(false);

        CandleDataSet set1=new CandleDataSet(list,"A股");

        set1.setDrawIcons(false);//
        set1.setShadowColor(Color.GRAY);   //
        set1.setShadowWidth(1.2f);     //
        set1.setDecreasingColor(Color.RED); //降色
        set1.setIncreasingColor(Color.GREEN); //增色
        set1.setIncreasingPaintStyle(Paint.Style.FILL); //降色是否填充
        set1.setDecreasingPaintStyle(Paint.Style.FILL);// 增色是否填充


        CandleData data=new CandleData(set1);
        candlechart.setData(data);
        candlechart.invalidate();

    }


}
